package com.guojian.common.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.ResourceUtils;

/**
 * 读取classpath下的配置文件
 * 
 * @author gj
 *
 */
public class ConfigContext {

	private static Properties PROPERTIES = new Properties();

	private static ConcurrentHashMap<String, String> sqlConfig = new ConcurrentHashMap<>();

	public static final PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();

	static {
		try {
			Resource[] proResources = pathMatchingResourcePatternResolver.getResources("classpath:*.properties");
			for (Resource resource : proResources) {
				PROPERTIES.load(resource.getInputStream());
			}
			Resource[] sqlResources = pathMatchingResourcePatternResolver.getResources("classpath:**/*.sql");
			for (Resource sqlResource : sqlResources) {
				String sqlContent = IOUtils.toString(sqlResource.getInputStream(), "UTF-8");
				sqlConfig.put(FilenameUtils.getBaseName(sqlResource.getFilename()), sqlContent);
			}
		} catch (Exception e) {
			throw new RuntimeException("ConfigContext: 读取配置文件异常," + e);
		}
	}

	public static Properties getConfigs() {
		return PROPERTIES;
	}

	public static String getProperty(String name) {
		return PROPERTIES.getProperty(name, "");
	}

	public static String getSql(String name) {
		return sqlConfig.get(name);
	}

	public static InputStream getClasspathResourceInputStream(String fileName) {
		try {
			return pathMatchingResourcePatternResolver.getResource("classpath:" + fileName).getInputStream();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) {
		// Map<String, String> valueMap = new HashMap<>();
		// valueMap.put("D1", "'2019-06-11'");
		// valueMap.put("D2", "'2019-06-11'");
		// Matcher matcher =
		// Pattern.compile("\\%(.*?)\\%").matcher(sqlConfig.get("0616"));
		// String content = "this is %D1% %D2% %shopid% ";
		// while (matcher.find()) {
		// String key1 = matcher.group(0);
		// String key2 = matcher.group(1);
		// System.out.println(key1 + "===" + key2);
		// // sqlConfig.get("0616").replaceAll(key2, valueMap.get(key1));
		// content = content.replaceAll(key1, valueMap.getOrDefault(key2, ""));
		// }
		// System.out.println(content);
		// System.out.println(getClasspathResourceInputStream("template/caiwu_jsno.xlsx"));

		try {
			File file = ResourceUtils.getFile("classpath:template/caiwu_jsno.xlsx");
			System.out.println(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

}
